import Api from "../../../../utils/http";

// pages/user/user/myUnit/myUnit.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    usertype: 1, //单位管理员状态1为用户，2为单位管理
    userInfo: '', //用户数据
    type: null, //状态值
  },
  //跳转申请单位
  toApplyUnit: function () {
    wx.navigateTo({
      url: '/pages/user/user/applyUnit/applyUnit',
    })
  },
  toswitch: function () {
    this.getRoleSwitch();

  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.getusertext();
    // that.getRoleSwitch();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    that.getusertext();
  },
  //个人信息详情
  getusertext: function () {
    var that = this;
    console.log(that.data)
    let uid = wx.getStorageSync('userid');
    var param = {
      uid: uid.id,
      user_type: uid.user_type,
    };
    console.log(param)
    Api.ajax("/app/Zpersonalcenter/userInfo", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        if (res.data.is_manage == 1) {
          that.setData({
            type: 3,
            usertype: 2
          })
        } else if (res.data.is_department == 1) {
          that.setData({
            type: 2,
            usertype: 3
          })
        } else {
          that.setData({
            usertype: 1,
          })
        }

      }
    })
  },
  //切换用户
  getRoleSwitch: function () {
    var that = this;
    let uid = wx.getStorageSync('userid');
    var param = {
      uid: uid.id,
      user_type: uid.user_type,
      type: that.data.type,
    }
    console.log(param)
    Api.ajax('/app/Zindexmanage/roleSwitch', param).then(res => {
      console.log(res)
      if (res.code == 200) {
        wx.setStorageSync('userid', res.data);
        that.setData({
          userInfo: res.data
        })

        if (res.data.is_manage == 1) {
          that.setData({
            usertype: 2,
          })
        } else if (res.data.is_department == 1) {
          that.setData({
            usertype: 3,
          })
        } else {
          that.setData({
            usertype: 1,
          })
        }
        wx.reLaunch({
          url: '/pages/index/index',
        })
      }


    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})